$(document).ready(function () {
    var str="";
    $("#insert").click(function () {
        var date = $("#tdate").val();

        var type = $("#ttype").val();
        if (type == 1) {
            addSelf();
        }
        else {
            addTeam();
        }

        function addSelf() {
        $.ajax({
            type: "POST",
            url: "/self/checkDate",
            dataType: "text",
            data: {
                date: date
            },
            success: function (data) {
                console.log(data);
                alert("result " + data);
                if ("success" == data) {
                    var date = $("#tdate").val();
                    var tableId = document.getElementById("tques");
                    var trs = tableId.rows;
                    var classId = $("#selectClazz").val();
                    if (trs.length == 1) {
                        alert("提交内容为空!!");
                        return;
                    }
                    for (var i = 1, len = trs.length; i < len; i++) {
                        var id = trs[i].cells[0].innerText;
                        str += id + ",";
                    }
                    $.ajax({
                        type: "POST",
                        url: "/self/insertTo",
                        dataType: "text",
                        data: {
                            questionId: str,
                            classId: classId,
                            date: date

                        },
                        success: function (data) {
                            console.log(data);
                            alert("reslut" + data);
                            if (1 == data) {
                                alert("添加成功!!!");
                                window.location.reload();
                            }
                            else {
                                alert("添加失败!!!")
                            }
                        }, error: function (xhr, textStatus) {
                            console.log('错误');
                            console.log(xhr);
                            console.log(textStatus);
                        }
                    });

                }
                else {
                    alert("今日已提交过作业!!")
                }

            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    }

    function  addTeam() {
        $.ajax({
            type: "POST",
            url: "/team/checkDate",
            dataType: "text",
            data: {
                date: date
            },
            success: function (data) {
                console.log(data);
                alert("result " + data);
                if ("success" == data) {
                    var date = $("#tdate").val();
                    var tableId = document.getElementById("tques");
                    var trs = tableId.rows;
                    var classId = $("#selectClazz").val();
                    if (trs.length == 1) {
                        alert("提交内容为空!!");
                        return;
                    }
                    for (var i = 1, len = trs.length; i < len; i++) {
                        var id = trs[i].cells[0].innerText;
                        str += id + ",";
                    }
                    $.ajax({
                        type: "POST",
                        url: "/team/insertTo",
                        dataType: "text",
                        data: {
                            questionId: str,
                            classId: classId,
                            date: date

                        },
                        success: function (data) {
                            console.log(data);
                            alert("reslut" + data);
                            if (1 == data) {
                                alert("添加成功!!!");
                                window.location.reload();
                            }
                            else {
                                alert("添加失败!!!")
                            }
                        }, error: function (xhr, textStatus) {
                            console.log('错误');
                            console.log(xhr);
                            console.log(textStatus);
                        }
                    });

                }
                else {
                    alert("今日已提交过作业!!")
                }

            },
            error: function (xhr, textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    }


    })


});